import { notification } from 'ant-design-vue';
import axios, { AxiosRequestConfig, Method } from "axios";
import appsettings from "../appsettings";
import { UserContext } from "./UserContext";
import { useRouter } from "vue-router";
import { ApiResult } from '@/models/ApiResult';

/**
 * http请求
 */
export class HttpClient {
    /**
     * htpp请求
     * @param url 地址
     * @param data 参数
     * @param method 请求方法
     * @param isAuthorize 是否添加授权请求头
     * @returns 
     */
    static request<T>(url: string, data: any, method: Method = "post", isAuthorize: boolean = true, headers: any = { Authorization: "" }): Promise<ApiResult<T>> {
        //const router = useRouter();

        headers["Request-Source"] = appsettings.RequestSource;

        return new Promise<ApiResult<T>>((resolve, reject) => {
            let requestConfig: AxiosRequestConfig = {
                baseURL: appsettings.ApiBaseURL,
                url: url,
                headers: headers,
                data: data,
                timeout: 0,
                method: method
            };
            if (isAuthorize) {
                if (!UserContext.isExist()) {

                    notification.warn({
                        message: '警告',
                        description: "登录过期",
                    });
                    //router.push("/user/login");
                    setTimeout(function () {
                        window.location.href = "/user/login";
                    }, 500)
                    return;
                }
                if (requestConfig.headers !== undefined) {
                    requestConfig.headers.Authorization = UserContext.getToken();
                }
            }
            axios.create(requestConfig).request(requestConfig).then(res => {
                if (res.status != 200) {
                    notification.error({
                        message: '警告',
                        description: String(res.data),
                    });
                    return;
                }

                let rc = res.data;

                let result: ApiResult<T> = {
                    code: rc.code,
                    msg: rc.msg,
                    data: rc.data as T
                };

                if (res.status == 200) {
                    if (result.code == 40001 || result.code == 40002) { //未登陆
                        UserContext.remove();
                        //router.push("/user/login")
                        setTimeout(function () {
                            //window.location.href = "/user/login";
                        }, 500)
                        return;
                    }

                    if (result.code == 500) {
                        notification.error({
                            message: '系统内部错误',
                            description: result.msg,
                        });
                        return;
                    }
                    resolve(result);
                }

            }).catch(err => {
                notification.error({
                    message: '系统内部错误',
                    description: String(err),
                });
            });
        });
    };

    /**
     * POST请求
     * @param url 请求地址
     * @param content 请求内容
     * @returns 
     */
    static async post<T>(url: string, content: {} | [] = {}, isAuthorize = true) {
        return await this.request<T>(url, content, "POST", isAuthorize);
    }

    /**
   * POST请求
   * @param url 请求地址
   * @param content 请求内容
   * @returns 
   */
    static async postBus(url: string, content: {} | [] = {}, isAuthorize = true) {
        return await this.request<any>(url, content, "POST", isAuthorize);
    }

    /**
     * POST请求获取data数据
     * @param url 请求地址
     * @param content 请求内容
     * @returns 
     */
    static async postAsResult<T>(url: string, content: {} | [] = {}, isAuthorize = true) {
        return (await this.post<T>(url, content, isAuthorize)).data;
    }

    /**
     * GET请求
     * @param url 请求地址 
     * @param content 请求参数
     * @returns 
     */
    static async get<T>(url: string, content: {} = {}, isAuthorize = true) {
        return await this.request<T>(url, content, "get", isAuthorize);
    }

    /**
  * GET请求
  * @param url 请求地址 
  * @param content 请求参数
  * @returns 
  */
    static async getBus(url: string, content: {} = {}, isAuthorize = true) {
        return await this.request<any>(url, content, "get", isAuthorize);
    }

    /**
    * GET请求
    * @param url 请求地址 
    * @param content 请求参数
    * @returns 
    */
    static async getAsResult<T>(url: string, content: {} = {}, isAuthorize = true) {
        return (await this.get<T>(url, content, isAuthorize)).data;
    }

    /**
     * 上传客户端
     * @param url 地址
     * @param formData 表单数据 
     * @returns 
     */
    static async uploadClient<T>(url: string, formData: FormData) {
        return await this.request<T>(url, formData, "POST", true, { 'Content-Type': 'multipart/form-data', Authorization: "" })
    }
}

